package avicit.cbb.techclass.tabledefine.commtempletmanager.action.rest;

import avicit.cbb.techclass.tabledefine.commtablemanager.domain.CbbTabColumns;
import avicit.cbb.techclass.tabledefine.commtablemanager.action.service.CommTableManagerService;
import avicit.cbb.techclass.tabledefine.commtempletmanager.domain.CbbTempletCode;
import avicit.cbb.techclass.tabledefine.commtempletmanager.domain.CbbTempletField;
import avicit.cbb.techclass.tabledefine.commtempletmanager.domain.CbbTempletInfo;
import avicit.cbb.techclass.tabledefine.commtempletmanager.action.service.CommTempletManagerService;
import avicit.platform6.api.session.SessionHelper;
import avicit.platform6.commons.utils.web.TreeNode;
import avicit.platform6.core.context.ThreadContextHelper;
import avicit.platform6.core.dao.Paging;
import avicit.platform6.core.domain.BeanBase;
import avicit.platform6.core.rest.msg.ResponseMsg;
import avicit.platform6.core.jdbc.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
import java.util.Vector;

/**
 * @金航数码科技有限责任公司
 * @作者：jiam
 * @邮箱：jiam@avic-digital.com
 * @创建时间： 2020-12-31 14:52
 * @类说明：oa_travel_rbRest
 * @修改记录：
 */
@RestController
@Api(tags = "CommTempletManagerService", description = "CommTemplet_Manager_Service")
@RequestMapping("/api/avicit/cbb/techclass/tabledefine/commtempletmanager/action/rest/CommTempletManagerRest")
public class CommTempletManagerRest {

    private static final Logger LOGGER = LoggerFactory.getLogger(CommTempletManagerRest.class);

    @Autowired
    private CommTempletManagerService commTempletManagerService;




    /**
     * 获取列最大排序值
     *
     * @param map
     *
     * @throws
     * @author 朱锋
     */
    @PostMapping("/getMaxOrderNum")
    @ApiOperation(value = "查询最大数")
    public ResponseMsg<Integer> getMaxOrderNum(@ApiParam(value = "模板id", name = "map") @RequestBody Map<String, Object> map) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
        try {
            String templetId = (String) map.get("templetId");
            String sysId = (String) map.get("sysId");
            responseMsg.setResponseBody(commTempletManagerService.getMaxOrderNum(templetId, sysId));
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    /**
     * 删除参考模板字段信息操作
     *
     * @param map
     * @return
     */
    @PostMapping("/deleteCommTempletField")
    @ApiOperation(value = "根据id删除字段")
    public ResponseMsg deleteCommTempletField(@ApiParam(value = "ids组", name = "ids") @RequestBody Map<String, Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        try {
            String ids = (String) map.get("ids");
            String sysId = (String) map.get("sysId");
            commTempletManagerService.deleteCommTempletField(ids, sysId);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    @GetMapping("/getCommTempletInfoById")
    @ApiOperation(value = "通过id查询单条记录")
    public ResponseMsg<CbbTempletInfo> getCommTempletInfoById(@ApiParam(value = "id", name = "id") @PathVariable("id") String id) {
        ResponseMsg<CbbTempletInfo> responseMsg = new ResponseMsg<>();
        try {
            CbbTempletInfo cbbTempletInfo = commTempletManagerService.getCommTempletInfoById(id);
//        if (cbbTempletInfo != null) {
//            valueConvert(Arrays.asList(oaTravelRb));
//        }
            responseMsg.setResponseBody(cbbTempletInfo);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    @PostMapping("/insertCommTempletFields")
    @ApiOperation(value = "添加字段")
    public ResponseMsg insertCommTempletFields(@ApiParam(value = "map", name = "map") @RequestBody Map<String, Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        try {
            CbbTempletField[] cbbTempltFields = (CbbTempletField[]) map.get("cbbTempltFields");
            String sysId = (String) map.get("sysId");
            String orgId = (String) map.get("orgId");
            String deptId = (String) map.get("deptId");
            String templetId = (String) map.get("templetId");
            commTempletManagerService.insertCommTempletFields(cbbTempltFields, sysId, orgId, deptId, templetId);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    @PostMapping("/updateCbbTempletFieldCodeArray")
    @ApiOperation(value = "更新字段")
    public ResponseMsg updateCbbTempletFieldCodeArray(@ApiParam(value = "map", name = "map") @RequestBody Map<String, Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        try {
            String updateCodeData = (String) map.get("updateCodeData");
            String templetId = (String) map.get("templetId");
            String fieldId = (String) map.get("fieldId");
            String sysId = (String) map.get("sysId");
            commTempletManagerService.updateCbbTempletFieldCodeArray(updateCodeData, templetId, fieldId, sysId);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    @PostMapping("/insertCbbTempletFieldCodeArray")
    @ApiOperation(value = "新新加字段")
    public ResponseMsg insertCbbTempletFieldCodeArray(@ApiParam(value = "map", name = "map") @RequestBody Map<String, Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        try {
            String inserCodeData = (String) map.get("inserCodeData");
            String sysId = (String) map.get("sysId");
            String orgId = (String) map.get("orgId");
            String deptId = (String) map.get("deptId");
            String templetId = (String) map.get("templetId");
            String fieldId = (String) map.get("fieldId");
            commTempletManagerService.insertCbbTempletFieldCodeArray(inserCodeData, sysId, orgId, deptId, templetId, fieldId);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    /**
     * 删除参考模板代码维护信息操作
     *
     * @param map
     * @return
     */
    @PostMapping("/deletecommTempletCodeBydeleteData")
    @ApiOperation(value = "新新加字段")
    public ResponseMsg deletecommTempletCodeBydeleteData(@ApiParam(value = "map", name = "map") @RequestBody Map<String, Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        try {
            String deleteData = (String) map.get("deleteData");
            String templetId = (String) map.get("templetId");
            String fieldId = (String) map.get("fieldId");
            String sysId = (String) map.get("sysId");
            commTempletManagerService.deletecommTempletCodeBydeleteData(deleteData, templetId, fieldId, sysId);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    /**
     * 删除参考模板基本信息
     *
     * @param map
     * @return
     */
    @PostMapping("/deleteCommTempletInfoById")
    @ApiOperation(value = "删除参考模板基本信息")
    public ResponseMsg deleteCommTempletInfoById(@ApiParam(value = "map", name = "map") @RequestBody Map<String, Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        try {
            String id = (String) map.get("id");
            String sysId = (String) map.get("sysId");
            commTempletManagerService.deleteCommTempletInfoById(id, sysId);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    @PostMapping("/getTempltIdByTempltCode")
    @ApiOperation(value = "新增对象")
    public ResponseMsg<String> getTempltIdByTempltCode(@ApiParam(value = "map", name = "map") @RequestBody Map<String, Object> map) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        String templtIdByTempltCode = null;
        try {
            String s = (String) map.get("s");
            String sysId = (String) map.get("sysId");
            templtIdByTempltCode = commTempletManagerService.getTempltIdByTempltCode(sysId + "_TEMPLATE", sysId);
            responseMsg.setResponseBody(templtIdByTempltCode);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }





    @PostMapping("/getParentNodeByParentId")
    @ApiOperation(value = "得到夫节点")
    public ResponseMsg<Integer> getParentNodeByParentId(@ApiParam(value = "得到夫节点", name = "root") @RequestBody String root) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
        try {
            responseMsg.setResponseBody(commTempletManagerService.getParentNodeByParentId("ROOT"));
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    @PostMapping("/saveRootLantopArchiveTabClassVByType")
    @ApiOperation(value = "获取顶层节点")
    public ResponseMsg saveRootLantopArchiveTabClassVByType(@ApiParam(value = "获取顶层节点", name = "deptId") @RequestBody String deptId) {
        ResponseMsg responseMsg = new ResponseMsg();
        try {
            commTempletManagerService.saveRootLantopArchiveTabClassVByType(deptId);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }


    @PostMapping("/getCommTempletInfoTreeNodesRoot")
    @ApiOperation(value = "根据orgId过滤过滤树节点")
    public ResponseMsg<List<TreeNode>> getCommTempletInfoTreeNodesRoot(@ApiParam(value = "根据orgId过滤过滤树节点", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<List<TreeNode>> responseMsg = new ResponseMsg<>();
        try {
            String id = (String) map.get("id");
            String sysId = (String) map.get("sysId");
            responseMsg.setResponseBody(commTempletManagerService.getCommTempletInfoTreeNodesRoot(id, sysId, "ORG_ROOT"));
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    @PostMapping("/exportExcelModule")
    @ApiOperation(value = "导出excel")
    public ResponseMsg exportExcelModule(@ApiParam(value = "导出excel", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        try {
            List<String> headValue = (List<String>) map.get("headValue");
            ByteArrayOutputStream os = (ByteArrayOutputStream) map.get("os");
            commTempletManagerService.exportExcelModule(headValue, os);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    @PostMapping("/getCbbTempletFields")
    @ApiOperation(value = "获取数据")
    public ResponseMsg<List<Map<String, Object>>> getCbbTempletFields(@ApiParam(value = "获取数据", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<List<Map<String, Object>>> responseMsg = null;
        try {
            responseMsg = new ResponseMsg<>();
            String templetId = (String) map.get("templetId");
            String orgId = (String) map.get("orgId");
            List<Map<String, Object>> cbbTempletFields = commTempletManagerService.getCbbTempletFields(templetId, orgId);
            responseMsg.setResponseBody(cbbTempletFields);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    @PostMapping("/getFileName")
    @ApiOperation(value = "得到文件名")
    public ResponseMsg<String> getFileName(@ApiParam(value = "得到文件名", name = "templetId") @RequestBody String templetId) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        try {
            // 总组织默认根节点ID是ORG_ROOT
            responseMsg.setResponseBody(commTempletManagerService.getFileName(templetId));
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    @PostMapping("/getPageCodeDetailList")
    @ApiOperation(value = "分页结果集")
    public ResponseMsg<Page<Map<String,Object>>> getPageCodeDetailList(@ApiParam(value = "分页结果集", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<Page<Map<String,Object>>> responseMsg = new ResponseMsg<>();
        try {
            Paging<CbbTempletCode>  page = (Paging<CbbTempletCode>) map.get("page");
            Map<String,Object> parameter = (Map<String, Object>) map.get("parameter");
            String sysId = (String) map.get("sysId");
            responseMsg.setResponseBody(commTempletManagerService.getPageCodeDetailList(page, parameter, sysId));
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }


    /**
     * 获取系统节点同模板对应的字段信息
     * @param map
     * @return
     */
    @PostMapping("/duplicateCheck")
    @ApiOperation(value = "获取系统节点同模板对应的字段信息")
    public ResponseMsg<String> duplicateCheck(@ApiParam(value = "获取系统节点同模板对应的字段信息", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        try {
            String sysId = (String) map.get("sysId");
            String orgId = (String) map.get("orgId");
            String deptId = (String) map.get("deptId");
            String sysNodeId = (String) map.get("sysNodeId");
            String tempModelType = (String) map.get("tempModelType");
            String nodeRows = (String) map.get("nodeRows");
            String treeType = (String) map.get("treeType");
            responseMsg.setResponseBody(commTempletManagerService.duplicateCheck(sysId, orgId, deptId, sysNodeId, tempModelType, nodeRows, treeType));
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    @PostMapping("/updateCommTempletInfo")
    @ApiOperation(value = "更新")
    public ResponseMsg updateCommTempletInfo(@ApiParam(value = "更新", name = "commTempletInfo") @RequestBody CbbTempletInfo commTempletInfo) {
        ResponseMsg responseMsg = new ResponseMsg();
        try {
            commTempletManagerService.updateCommTempletInfo(commTempletInfo);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    @PostMapping("/updateDetailCommTempletFields")
    @ApiOperation(value = "更新")
    public ResponseMsg updateDetailCommTempletFields(@ApiParam(value = "更新", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        try {
            CbbTempletField[] cbbTempltFields = (CbbTempletField[]) map.get("cbbTempltFields");
            String sysId = (String) map.get("sysId");
            commTempletManagerService.updateDetailCommTempletFields(cbbTempltFields, sysId);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    @PostMapping("/insertCommTempletInfo")
    @ApiOperation(value = "添加")
    public ResponseMsg insertCommTempletInfo(@ApiParam(value = "添加", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        try {
            CbbTempletInfo commTempletInfo = (CbbTempletInfo) map.get("commTempletInfo");
            String orgId = (String) map.get("orgId");
            String deptId = (String) map.get("deptId");
            commTempletManagerService.insertCommTempletInfo(commTempletInfo, orgId, deptId);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    @PostMapping("/getPageList")
    @ApiOperation(value = "得到全部结果")
    public ResponseMsg<Paging<CbbTempletInfo>> getPageList(@ApiParam(value = "得到全部结果", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<Paging<CbbTempletInfo>> responseMsg = new ResponseMsg<>();
        try {
            Paging<CbbTempletInfo> page = (Paging<CbbTempletInfo>) map.get("page");
            Map<String,Object> parameter = (Map<String, Object>) map.get("parameter");
            String sysId = (String) map.get("sysId");
            Paging<CbbTempletInfo> pageList = commTempletManagerService.getPageList(page, parameter, sysId);
            responseMsg.setResponseBody(pageList);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }


    @PostMapping("/updateCbbTempletFieldArray")
    @ApiOperation(value = "更新")
    public ResponseMsg updateCbbTempletFieldArray(@ApiParam(value = "更新", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        try {
            CbbTempletField[] ccArray = (CbbTempletField[]) map.get("ccArray");
            String templetId = (String) map.get("templetId");
            String sysId = (String) map.get("sysId");
            commTempletManagerService.updateCbbTempletFieldArray(ccArray, templetId, sysId);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    @PostMapping("/insertCbbTempletFieldArray")
    @ApiOperation(value = "添加")
    public ResponseMsg insertCbbTempletFieldArray(@ApiParam(value = "添加", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        try {
            CbbTempletField[] ccArray = (CbbTempletField[]) map.get("ccArray");
            String sysId = (String) map.get("sysId");
            String orgId = (String) map.get("orgId");
            String deptId = (String) map.get("deptId");
            String templetId = (String) map.get("templetId");
            commTempletManagerService.insertCbbTempletFieldArray(ccArray, sysId, orgId, deptId, templetId);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    @PostMapping("/getPageRuleDetailList")
    @ApiOperation(value = "得到结果集")
    public ResponseMsg<Page<Map<String, Object>>> getPageRuleDetailList(@ApiParam(value = "得到结果集", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<Page<Map<String, Object>>> responseMsg = new ResponseMsg<>();
        try {
            Paging<CbbTempletCode> page = (Paging<CbbTempletCode>) map.get("page");
            Map<String,Object> parameter = (Map<String, Object>) map.get("parameter");
            responseMsg.setResponseBody(commTempletManagerService.getPageRuleDetailList(page, parameter));
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    @PostMapping("/checkUnique")
    @ApiOperation(value = "选择编码")
    public ResponseMsg<Boolean> checkUnique(@ApiParam(value = "选择编码", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        try {
            String name = (String) map.get("name");
            String id = (String) map.get("id");
            String sysId = (String) map.get("sysId");
            boolean b = commTempletManagerService.checkUnique(name, id, sysId);
            responseMsg.setResponseBody(b);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    @PostMapping("/checkCodeUnique")
    @ApiOperation(value = "选择编码")
    public ResponseMsg<Integer> checkCodeUnique(@ApiParam(value = "得到结果集", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
        try {
            String lookupName = (String) map.get("lookupName");
            String lookupCode = (String) map.get("lookupCode");
            String id = (String) map.get("id");
            String tid = (String) map.get("tid");
            String fid = (String) map.get("fid");
            String sysId = (String) map.get("sysId");
            int i = commTempletManagerService.checkCodeUnique(lookupName, lookupCode, id, tid, fid, sysId);
            responseMsg.setResponseBody(i);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    @PostMapping("/insertSysCode")
    @ApiOperation(value = "添加编码")
    public ResponseMsg insertSysCode(@ApiParam(value = "添加编码", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        try {
            String cid = (String) map.get("cid");
            String tid = (String) map.get("tid");
            String fid = (String) map.get("fid");
            String sysId = (String) map.get("sysId");
            String orgId = (String) map.get("orgId");
            String deptId = (String) map.get("deptId");
            commTempletManagerService.insertSysCode(cid, tid, fid, sysId, orgId, deptId);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    @PostMapping("/getPageSysCodeSonList")
    @ApiOperation(value = "结果集")
    public ResponseMsg<Page<Map<String, Object>>> getPageSysCodeSonList(@ApiParam(value = "结果集", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<Page<Map<String, Object>>> responseMsg = new ResponseMsg<>();
        try {
            Paging<CbbTempletCode> page = (Paging<CbbTempletCode>) map.get("page");
            Map<String, Object> parameter = (Map<String, Object>) map.get("parameter");
            String sysId = (String) map.get("sysId");
            Page<Map<String, Object>> pageSysCodeSonList = commTempletManagerService.getPageSysCodeSonList(page, parameter, sysId);
            responseMsg.setResponseBody(pageSysCodeSonList);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }


    /**
     * 根据orgId过滤过滤树节点
     * @param map
     * @return
     */
    @PostMapping("/getCommTempletInfoTreeNodes")
    @ApiOperation(value = "根据orgId过滤过滤树节点")
    public ResponseMsg<List<TreeNode>> getCommTempletInfoTreeNodes(@ApiParam(value = "根据orgId过滤过滤树节点", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<List<TreeNode>> responseMsg = new ResponseMsg<>();
        try {
            String id = (String) map.get("id");
            String sysId = (String) map.get("sysId");
            String orgId = (String) map.get("orgId");
            List<TreeNode> commTempletInfoTreeNodes = commTempletManagerService.getCommTempletInfoTreeNodes(id, sysId, orgId);
            responseMsg.setResponseBody(commTempletInfoTreeNodes);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }


    /**
     * 唯一性验证【名称唯一性验证】
     * @param map
     * @return
     */
    @PostMapping("/checkFieldUnique")
    @ApiOperation(value = "唯一性验证【名称唯一性验证】")
    public ResponseMsg<Boolean> checkFieldUnique(@ApiParam(value = "唯一性验证【名称唯一性验证】", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        try {
            String name = (String) map.get("name");
            String tId = (String) map.get("tId");
            String id = (String) map.get("id");
            String sysId = (String) map.get("sysId");
            boolean b = commTempletManagerService.checkFieldUnique(name, tId, id, sysId);
            responseMsg.setResponseBody(b);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    /**
     * 更新字段
     * @param map
     */
    @PostMapping("/updateDetailCommTempletField")
    @ApiOperation(value = "更新字段")
    public ResponseMsg updateDetailCommTempletField(@ApiParam(value = "更新字段", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        try {
            BeanBase commTempletField = (BeanBase) map.get("commTempletField");
            String sysId = (String) map.get("sysId");
            commTempletManagerService.updateDetailCommTempletField(commTempletField, sysId);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    /**
     * 添加字段
     * @param map
     */
    @PostMapping("/insertCommTempletField")
    @ApiOperation(value = "添加字段")
    public ResponseMsg insertCommTempletField(@ApiParam(value = "添加字段", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg responseMsg  = new ResponseMsg();
        try {
            CbbTempletField commTempletField = (CbbTempletField) map.get("commTempletField");
            String sysId = (String) map.get("sysId");
            String orgId = (String) map.get("orgId");
            String deptId = (String) map.get("deptId");
            commTempletManagerService.insertCommTempletField(commTempletField, sysId, orgId, deptId);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;
    }

    /**
     * 数据没有重复,调用添加方法
     * @param map
     */
    @PostMapping("/insertCommTempletInitFields")
    @ApiOperation(value = "数据没有重复,调用添加方法")
    public ResponseMsg insertCommTempletInitFields(@ApiParam(value = "数据没有重复,调用添加方法", name = "map") @RequestBody Map<String,Object> map) {
        ResponseMsg responseMsg  = new ResponseMsg();
        try {
            CbbTempletField[] cbbTempltFields = (CbbTempletField[]) map.get("cbbTempltFields");
            String sysId = (String) map.get("sysId");
            String orgId = (String) map.get("orgId");
            String deptId = (String) map.get("deptId");
            String templetId = (String) map.get("templetId");
            commTempletManagerService.insertCommTempletInitFields(cbbTempltFields, sysId, ThreadContextHelper.getOrgId(), ThreadContextHelper.getDeptId(), templetId);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;

    }


    /**
     * 参考模板字段信息跳转编辑取值
     *
     * @param id
     * @return
     * @throws Exception
     */
    @PostMapping("/getCommTempletFieldById")
    @ApiOperation(value = "参考模板字段信息跳转编辑取值")
    public ResponseMsg<CbbTempletField> getCommTempletFieldById(@ApiParam(value = "参考模板字段信息跳转编辑取值", name = "id") @RequestBody String id) {

            ResponseMsg<CbbTempletField> responseMsg = new ResponseMsg<>();
        try {
            CbbTempletField commTempletFieldById = commTempletManagerService.getCommTempletFieldById(id);
            responseMsg.setResponseBody(commTempletFieldById);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;

    }

    /**
     * 获取平台通用代码信息
     * @param map
     * @return
     */
    @PostMapping("/getPageSysCodeDetailList")
    @ApiOperation(value = "获取平台通用代码信息")
    public ResponseMsg<Page<Map<String, Object>>> getPageSysCodeDetailList(@ApiParam(value = "获取平台通用代码信息", name = "map") @RequestBody Map<String,Object> map) {

        ResponseMsg<Page<Map<String, Object>>> responseMsg = new ResponseMsg<>();
        Page<Map<String, Object>> pageSysCodeDetailList = null;
        try {
            Paging<CbbTempletCode> page = (Paging<CbbTempletCode>) map.get("page");
            Map<String,Object> parameter = (Map<String, Object>) map.get("parameter");
            String sysId = (String) map.get("sysId");
            pageSysCodeDetailList = commTempletManagerService.getPageSysCodeDetailList(page, parameter, sysId);
            responseMsg.setResponseBody(pageSysCodeDetailList);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;

    }


    /**
     * 根据组织ID获取列名
     * @param map
     * @return
     */
    @PostMapping("/getCommTempletFieldByTempletId")
    @ApiOperation(value = "根据组织ID获取列名")
    public ResponseMsg<List<Map<String, Object>>> getCommTempletFieldByTempletId(@ApiParam(value = "根据组织ID获取列名", name = "map") @RequestBody Map<String,Object> map) {

        ResponseMsg<List<Map<String, Object>>> responseMsg = new ResponseMsg<>();
        Page<Map<String, Object>> pageSysCodeDetailList = null;
        try {
            String orgId = (String) map.get("orgId");
            String templetModelType = (String) map.get("templetModelType");
            String templetId = (String) map.get("templetId");
            List<Map<String, Object>> commTempletFieldByTempletId = commTempletManagerService.getCommTempletFieldByTempletId(orgId, templetModelType, templetId);
            responseMsg.setResponseBody(commTempletFieldByTempletId);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;

    }


    /**
     * 根据参考模板代码维护ID查询参考模板代码维护数据
     * @param map
     * @return
     */
    @PostMapping("/getCommTempletCodeById")
    @ApiOperation(value = "根据参考模板代码维护ID查询参考模板代码维护数据")
    public ResponseMsg<CbbTempletCode> getCommTempletCodeById(@ApiParam(value = "根据参考模板代码维护ID查询参考模板代码维护数据", name = "map") @RequestBody Map<String,Object> map) {

        ResponseMsg<CbbTempletCode> responseMsg = new ResponseMsg<>();
        try {
            String id = (String) map.get("id");
            CbbTempletCode commTempletCodeById = commTempletManagerService.getCommTempletCodeById(id);
            responseMsg.setResponseBody(commTempletCodeById);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;

    }


    /**
     *导出结构数据
     * @param map
     * @return
     */
    @PostMapping("/exportData")
    @ApiOperation(value = "导出结构数据")
    public ResponseMsg exportData(@ApiParam(value = "导出结构数据", name = "map") @RequestBody Map<String,Object> map) {

        ResponseMsg responseMsg = new ResponseMsg<>();
        try {
            List<String> headValue = (List<String>) map.get("headValue");
            List<String> head = (List<String>) map.get("head");
            List<Map<String, Object>> datas = (List<Map<String, Object>>) map.get("datas");
            ByteArrayOutputStream os = (ByteArrayOutputStream) map.get("os");
            commTempletManagerService.exportData(headValue, head, datas, os);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;

    }


    /**
     * 解析 excel2003 文件
     * @param map
     * @return
     */
    @PostMapping("/readXls")
    @ApiOperation(value = "解析 excel2003 文件")
    public ResponseMsg<Map<String, Vector<Vector<String>>>> readXls(@ApiParam(value = "解析 excel2003 文件", name = "map") @RequestBody Map<String,Object> map) {

        ResponseMsg<Map<String, Vector<Vector<String>>>> responseMsg = new ResponseMsg<>();
        try {
            String filePath = (String) map.get("filePath");
            Map<String, Vector<Vector<String>>> stringVectorMap = commTempletManagerService.readXls(filePath);
            responseMsg.setResponseBody(stringVectorMap);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;

    }


    /**
     * 解析 excel2007 文件
     * @param map
     * @return
     */
    @PostMapping("/readXlsx")
    @ApiOperation(value = "解析 excel2007 文件")
    public ResponseMsg<Map<String, Vector<Vector<String>>>> readXlsx(@ApiParam(value = "解析 excel2007 文件", name = "map") @RequestBody Map<String,Object> map) {

        ResponseMsg<Map<String, Vector<Vector<String>>>> responseMsg = new ResponseMsg<>();
        try {
            String filePath = (String) map.get("filePath");
            Map<String, Vector<Vector<String>>> stringVectorMap = commTempletManagerService.readXlsx(filePath);
            responseMsg.setResponseBody(stringVectorMap);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;

    }


    /**
     * 判断excel文件数据合法性
     * @param map
     * @return
     */
    @PostMapping("/testExcel")
    @ApiOperation(value = "判断excel文件数据合法性")
    public ResponseMsg testExcel(@ApiParam(value = "判断excel文件数据合法性", name = "map") @RequestBody Map<String,Object> map) {

        ResponseMsg responseMsg = new ResponseMsg<>();
        try {
            Vector<Vector<String>> datas = (Vector<Vector<String>>) map.get("datas");
            String templetId = (String) map.get("templetId");
            commTempletManagerService.testExcel(datas, templetId);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;

    }


    /**
     * 将excel中的数据添加到数据库中
     * @param map
     * @return
     */
    @PostMapping("/insertFromExcel")
    @ApiOperation(value = "将excel中的数据添加到数据库中")
    public ResponseMsg insertFromExcel(@ApiParam(value = "将excel中的数据添加到数据库中", name = "map") @RequestBody Map<String,Object> map) {

        ResponseMsg responseMsg = new ResponseMsg<>();
        try {
            Vector<Vector<String>> datas = (Vector<Vector<String>>) map.get("datas");
            String sysId = (String) map.get("sysId");
            String currentOrgId = (String) map.get("currentOrgId");
            String currentDeptId = (String) map.get("currentDeptId");
            String templetId = (String) map.get("templetId");
            commTempletManagerService.insertFromExcel(datas, sysId, currentOrgId, currentDeptId, templetId);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;

    }


    /**
     * 初始化字段维护导入判断excel文件数据合法性
     * @param map
     * @return
     */
    @PostMapping("/initFieldTestExcel")
    @ApiOperation(value = "将excel中的数据添加到数据库中")
    public ResponseMsg initFieldTestExcel(@ApiParam(value = "将excel中的数据添加到数据库中", name = "map") @RequestBody Map<String,Object> map) {

        ResponseMsg responseMsg = new ResponseMsg<>();
        try {
            Vector<Vector<String>> datas = (Vector<Vector<String>>) map.get("datas");
            String templetId = (String) map.get("templetId");
            commTempletManagerService.initFieldTestExcel(datas, templetId);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;

    }


    /**
     * 初始化字段维护导入将excel中的数据添加到数据库中
     * @param map
     * @return
     */
    @PostMapping("/initFieldInsertFromExcel")
    @ApiOperation(value = "初始化字段维护导入将excel中的数据添加到数据库中")
    public ResponseMsg initFieldInsertFromExcel(@ApiParam(value = "初始化字段维护导入将excel中的数据添加到数据库中", name = "map") @RequestBody Map<String,Object> map) {

        ResponseMsg responseMsg = new ResponseMsg<>();
        try {
            Vector<Vector<String>> datas = (Vector<Vector<String>>) map.get("datas");
            String sysId = (String) map.get("sysId");
            String currentOrgId = (String) map.get("currentOrgId");
            String currentDeptId = (String) map.get("currentDeptId");
            String templetId = (String) map.get("templetId");
            commTempletManagerService.initFieldInsertFromExcel(datas, sysId, currentOrgId, currentDeptId, templetId);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;

    }


    /**
     * 根据参考模板类型信息获得当前组织的参考模板信息和根组织的参考模板信息
     * @param map
     * @return
     */
    @PostMapping("/getCommTempletByTempModelType")
    @ApiOperation(value = "根据参考模板类型信息获得当前组织的参考模板信息和根组织的参考模板信息")
    public ResponseMsg<List<Map<String, Object>>> getCommTempletByTempModelType(@ApiParam(value = "根据参考模板类型信息获得当前组织的参考模板信息和根组织的参考模板信息", name = "map") @RequestBody Map<String,Object> map) {

        ResponseMsg<List<Map<String, Object>>> responseMsg = new ResponseMsg<>();
        try {
            String sysId = (String) map.get("sysId");
            String tempModelType = (String) map.get("tempModelType");
            String orgId = (String) map.get("orgId");
            List<Map<String, Object>> commTempletByTempModelType = commTempletManagerService.getCommTempletByTempModelType(sysId, orgId, tempModelType);
            responseMsg.setResponseBody(commTempletByTempModelType);
            responseMsg.setRetCode("200");
        } catch (Exception e) {
            responseMsg.setRetCode("201");
            responseMsg.setErrorDesc(e.getMessage());
            e.printStackTrace();
        }
        return responseMsg;

    }


}

